package com.ahcloud.edas.gatewaylog.server.configuration;

import com.ahcloud.edas.common.kafka.KafkaEnhanceProperties;
import com.ahcloud.edas.common.kafka.client.KafkaMessageClient;
import com.ahcloud.edas.gatewaylog.server.kafka.KafkaBootstrap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: edas
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/7 10:02
 **/
@Configuration
@EnableConfigurationProperties(KafkaEnhanceProperties.class)
public class GatewaylogServerConfiguration {

    @Bean
    public KafkaBootstrap kafkaBootstrap() {
        return new KafkaBootstrap();
    }

    @Bean
    public KafkaMessageClient kafkaMessageClient(KafkaEnhanceProperties kafkaProperties) {
        return new KafkaMessageClient(getEnv(), kafkaProperties.getClientId(), kafkaProperties.getBootstrapServers(), kafkaProperties.getProducer().buildProperties(), kafkaProperties.getConsumer().buildProperties());
    }

    private String getEnv() {
        String property = System.getProperty("spring.profiles.active");
        if (StringUtils.isBlank(property)) {
            property = System.getProperty("kafka.server.env", "dev");
        }
        return property;
    }
}
